package zzzhc.image.ui;

import java.awt.BorderLayout;
import java.awt.Event;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;

import zzzhc.image.model.AppModel;
import zzzhc.image.model.CaptureTableModel;

public class Gui extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JMenuBar jJMenuBar = null;

	private JMenu jMenu = null;

	private JMenuItem openMenuItem = null;

	private JTabbedPane jTabbedPane = null;

	private JMenuItem newItem = null;

	private JMenu helpMenu = null;

	private JMenuItem saveItem = null;

	private CapturePanel capturePanel = null;

	private OperatePanel operatePanel = null;

	private StatusBar statusBar = null;

	private JMenuItem exitMenuItem = null;

	private TrainerPanel trainerPanel = null;

	private RecognizeTestPanel recognizeTestPanel = null;

	private JMenuItem aboutMenuItem = null;

	/**
	 * This is the default constructor
	 */
	public Gui() {
		super();
		initialize();
		CaptureTableModel ctm = capturePanel.getCapturePanelModel()
				.getCaptureTableModel();
		operatePanel.getModel().setCaptureTableModel(ctm);
		trainerPanel.getModel().setOpPanelModel(operatePanel.getModel());
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 450);
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getJContentPane());
		this.setTitle("图象识别训练器");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		UIUtil.centerWindow(this);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJTabbedPane(), BorderLayout.CENTER);
			jContentPane.add(getStatusBar(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar
	 * 
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenu());
			jJMenuBar.add(getHelpMenu());

		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenu() {
		if (jMenu == null) {
			jMenu = new JMenu();
			jMenu.setText("文件");
			jMenu.add(getNewItem());
			jMenu.add(getOpenMenuItem());
			jMenu.add(getSaveItem());
			jMenu.add(getExitMenuItem());
		}
		return jMenu;
	}

	/**
	 * This method initializes openMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getOpenMenuItem() {
		if (openMenuItem == null) {
			openMenuItem = new JMenuItem();
			openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
					Event.ALT_MASK, false));
			openMenuItem.setText("打开");
			openMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");
					JFileChooser chooser = new JFileChooser();
					chooser.setCurrentDirectory(new File("."));
					chooser.setFileFilter(new ZIRFileFilter());
					chooser.setDialogType(JFileChooser.OPEN_DIALOG);
					int ret = chooser.showOpenDialog(Gui.this);
					if (ret == JFileChooser.APPROVE_OPTION) {
						File f = chooser.getSelectedFile();
						try {
							AppModel.getInstance().load(f);
						} catch (Exception ex) {
							JOptionPane.showConfirmDialog(Gui.this, "打开失败:"
									+ ex.getMessage(), "Error",
									JOptionPane.YES_OPTION);
						}
					}
				}
			});
		}
		return openMenuItem;
	}

	/**
	 * This method initializes jTabbedPane
	 * 
	 * @return javax.swing.JTabbedPane
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("样本获取", null, getCapturePanel(), null);
			jTabbedPane.addTab("处理规则", null, getOpPanel(), null);
			jTabbedPane.addTab("网络训练", null, getTrainerPanel(), null);
			jTabbedPane.addTab("识别测试", null, getRecognizeTestPanel(), null);
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes newItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getNewItem() {
		if (newItem == null) {
			newItem = new JMenuItem();
			newItem.setText("新建");
			newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
					Event.ALT_MASK, false));
			newItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (AppModel.getInstance().isSaved() == false) {
						save();
					}
					AppModel.getInstance().clear();
				}
			});
		}
		return newItem;
	}

	/**
	 * This method initializes helpMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("帮助");
			helpMenu.add(getAboutMenuItem());
		}
		return helpMenu;
	}

	/**
	 * This method initializes saveItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getSaveItem() {
		if (saveItem == null) {
			saveItem = new JMenuItem();
			saveItem.setText("保存");
			saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
					Event.ALT_MASK, false));
			saveItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					save();
				}
			});
		}
		return saveItem;
	}

	/**
	 * This method initializes capturePanel
	 * 
	 * @return zzzhc.image.ui.CapturePanel
	 */
	private CapturePanel getCapturePanel() {
		if (capturePanel == null) {
			capturePanel = new CapturePanel();
		}
		return capturePanel;
	}

	/**
	 * This method initializes operatePanel
	 * 
	 * @return zzzhc.image.ui.OperatePanel
	 */
	private OperatePanel getOpPanel() {
		if (operatePanel == null) {
			operatePanel = new OperatePanel();
		}
		return operatePanel;
	}

	/**
	 * This method initializes statusBar
	 * 
	 * @return zzzhc.image.ui.StatusBar
	 */
	private StatusBar getStatusBar() {
		if (statusBar == null) {
			statusBar = new StatusBar();
		}
		return statusBar;
	}

	/**
	 * This method initializes exitMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("退出");
			exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes trainerPanel
	 * 
	 * @return zzzhc.image.ui.TrainerPanel
	 */
	private TrainerPanel getTrainerPanel() {
		if (trainerPanel == null) {
			trainerPanel = new TrainerPanel();
		}
		return trainerPanel;
	}

	/**
	 * This method initializes recognizeTestPanel
	 * 
	 * @return zzzhc.image.ui.RecognizeTestPanel
	 */
	private RecognizeTestPanel getRecognizeTestPanel() {
		if (recognizeTestPanel == null) {
			recognizeTestPanel = new RecognizeTestPanel();
		}
		return recognizeTestPanel;
	}

	/**
	 * This method initializes aboutMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem();
			aboutMenuItem.setText("关于");
			aboutMenuItem
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							AboutDialog dialog = new AboutDialog(Gui.this);
							dialog.setModal(true);
							dialog.setVisible(true);
						}
					});
		}
		return aboutMenuItem;
	}

	private void save() {
		JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(new File("."));
		chooser.setFileFilter(new ZIRFileFilter());
		int retVal = chooser.showSaveDialog(Gui.this);
		if (retVal == JFileChooser.APPROVE_OPTION) {
			File f = chooser.getSelectedFile();
			try {
				AppModel.getInstance().save(f);
			} catch (Exception ex) {
				JOptionPane.showConfirmDialog(Gui.this, "保存失败:"
						+ ex.getMessage(), "Error", JOptionPane.YES_OPTION);
			}
		}
	}

	private static Gui instance;

	public synchronized static final Gui getInstance() {
		if (instance == null) {
			instance = new Gui();
		}
		return instance;
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		Gui gui = getInstance();
		gui.setVisible(true);
	}

	public static class ZIRFileFilter extends FileFilter {

		@Override
		public boolean accept(File f) {
			if (f.isDirectory()) {
				return true;
			}
			if (f.isFile() && f.getName().endsWith(".zir")) {
				return true;
			} else {
				return false;
			}
		}

		@Override
		public String getDescription() {
			return "*.zir";
		}

	}

} // @jve:decl-index=0:visual-constraint="10,10"
